package com.mes.schedule.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * date: Jun 22, 2015 5:31:58 PM <br/>  
 * @author Administrator  
 * @version   
 * @since JDK 1.6
 */
public class CombinedResource extends Resource implements Serializable {
	private final static Log log = LogFactory.getLog(CombinedResource.class);
	protected List<Resource> subResourceList; 

	public Resource mainResource;

	@Override
	public SOperationTask getSegementTask(int segement) {
		// TODO Auto-generated method stub
		Resource mainResource = this.getMainResource();
		if (mainResource != null)
			return mainResource.getSegementTask(segement);
		return null;
	}

	public Resource getMainResource() {
		return mainResource == null ? subResourceList.get(0) : mainResource;
	}

	public void setMainResource(Resource mainResource) {
		this.mainResource = mainResource;
	}

	public CombinedResource(List<Resource> subResourceList) {
		super();
		this.subResourceList = subResourceList;
	}

	public CombinedResource() {

		super();
		subResourceList = new ArrayList<Resource>();
	}




	@Override
	public synchronized int getSegementUse(int segement) {

		int segementUse = RESOURCE_IDLE;
		for (Resource resource : subResourceList) {
			int subUse = resource.getSegementUse(segement);
			if (subUse == RESOURCE_PROCESS) {
				segementUse = RESOURCE_PROCESS;
				break;
			} else if (subUse == RESOURCE_SETUP) {
				segementUse = RESOURCE_SETUP;
			}
		}
		return segementUse;

	}

	@Override
	public synchronized void setSegementUse(int segement, int useState,
			SOperationTask segementTask) {

		for (Resource resource : subResourceList) {
			resource.setSegementUse(segement, useState, segementTask);

		}

	}

	public List<Resource> getSubResourceList() {
		return subResourceList;
	}

	public void setSubResourceList(List<Resource> subResourceList) {
		this.subResourceList = subResourceList;
	}

	@Override
	public void destroy() {
		if (subResourceList != null) {
			subResourceList.clear();
			subResourceList = null;
		}

		if (mainResource != null)
		{
			mainResource = null;
		}
	}

}
